package com.ophiux.websocket.controller;

import com.ophiux.websocket.model.InMessage;
import com.ophiux.websocket.model.OutMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;

/**
 * @desc: 简单版单人聊天
 * @author: hhl
 * @date：2023/6/20 17:13
 */
@Controller
public class V3ChatRoomController {
    @Autowired
    private SimpMessagingTemplate template;

    @MessageMapping("/v3/single/chat")
    public void singleChat(InMessage message) {
        this.sendChatMessage(message);
    }

    /**
     * 发送聊天消息
     * @param message
     */
    public void sendChatMessage(InMessage message) {
        OutMessage outMessage = new OutMessage();
        BeanUtils.copyProperties(message, outMessage);
        outMessage.setTime(LocalDateTime.now());
        outMessage.setContent(message.getFrom()+" 发送："+message.getContent());
        //发送消息
        template.convertAndSend("/chat/single/" + message.getTo(), outMessage);
    }
}